#!/usr/bin/Rscript

##
## 

library(DBI)
library(RSQLite)
library(plyr)
library(grid)
library(gridExtra)
library(ggplot2)

primary_database <- "m5s_blog_mar2015.sqlite"
cut_date <- as.Date("2015-03-01")

# Functions
sqLiteConnect <- function(database, table) {
  require(DBI)
  con <- dbConnect(RSQLite::SQLite(), dbname = database)
  query <- dbSendQuery(con, paste("SELECT * FROM ", table, ";", sep="")) 
  result <- fetch(query, n = -1)
  dbClearResult(query)
  dbDisconnect(con)
  return(result)
}

blog_post <- sqLiteConnect(paste0(data_path, primary_database), "post")

blog_comment <- sqLiteConnect(paste0(data_path, primary_database), "comment")

week_vector <- seq(from = as.Date("2005-02-01"), to = as.Date(cut_date - 7), by = 7)

# Estimate number of users

# n_users <- sum(sapply(unique(tolower(blog_comment$username)), isComparable))
# n_users_by_week <- numeric()
# 
# 
# for (i in 1:length(week_vector)) {
#   n_users_by_week <- c(n_users_by_week, 
#                        sum(sapply(unique(tolower(subset(blog_comment,
#                                                         as.Date(created, origin="1970-01-01") >= week_vector[i] &
#                                                         as.Date(created, origin="1970-01-01") < week_vector[i+1]
#                                                         )$username)), isComparable)))
# }
 
##

# Beginning plot script

post.as.date = as.Date(blog_post[['date']])
comment.as.date = as.Date(blog_comment[['created']])


blog.post.week <- as.data.frame(table(cut(post.as.date, breaks = week_vector)))
blog.comment.week <- as.data.frame(table(cut(comment.as.date, breaks = week_vector)))

save(blog.post.week, file="02_06_m5s_blog_mar2015_post.RData")
save(blog.comment.week, file="02_06_m5s_blog_mar2015_comment.RData")
